package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.CodeA2Dao;
import com.int_tech.MessageCodeAll.entity.CodeA2;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * Created by liubochao on 2018/6/29.
 */
@Component
public class CheckMessionStartAndEndTimeTask {

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private CodeA2Dao codeA2Dao;

    /**
     * 任务起点开关门时间
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionStart() throws ParseException {
        //先获取未完成的任务
        List<Mession> messionList = messionDao.findByIsOverAndCheckStatus(0,0);

        for(Mession mession : messionList)
        {
            mession.setCheckStatus(1);
            try
            {
                List<CodeA2> codeA2List = codeA2Dao.findByCarNumberAndStatusAndCreateTimeBefore(mession.getCarNumber(),1,mession.getCreateTime());

                int so = 0;

                int sc = 0;

                for (CodeA2 codeA2 : codeA2List)
                {
                    if(so == 1 && sc == 1)
                    {
                        break;
                    }
                    else
                    {
                        //拿start,先拿关门的
                        if(codeA2.getOpenStatus() == 1)
                        {
                            //关门
                            mession.setStartCloseDoorTime(codeA2.getCreatedTime());
                            so = 1;
                            messionDao.save(mession);
                        }
                        else if(codeA2.getOpenStatus() == 0)
                        {
                            //开门,时间要小于关门
                            if(mession.getStartCloseDoorTime() == null)
                            {

                            }
                            else
                            {
                                if(codeA2.getCreatedTime().before(mession.getStartCloseDoorTime()))
                                {
                                    mession.setStartOpenDoorTime(codeA2.getCreatedTime());
                                    mession.setStartAddress(codeA2.getLocation());
                                    sc = 1;
                                    messionDao.save(mession);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                mession.setYiChang(1);
            }
            messionDao.save(mession);
        }
    }


    /**
     * 任务终点开关门时间
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionEnd() throws ParseException {

        //先获取未完成的任务
        List<Mession> messionList = messionDao.findByIsOverAndCheckStatus(0,1);

        for(Mession mession : messionList)
        {
            mession.setCheckStatus(2);
            try
            {
                List<CodeA2> codeA2List = codeA2Dao.findByCarNumberAndStatusAndCreateTimeAfter(mession.getCarNumber(),1,mession.getCreateTime());

                int eo = 0;

                int ec = 0;

                for (CodeA2 codeA2 : codeA2List)
                {
                    if(eo == 1 && ec == 1)
                    {
                        break;
                    }
                    else
                    {
                        //拿end,先拿开门的
                        if(codeA2.getOpenStatus() == 0)
                        {
                            //开门
                            mession.setEndOpenDoorTime(codeA2.getCreatedTime());
                            mession.setEndAddress(codeA2.getLocation());
                            ec = 1;
                            messionDao.save(mession);
                        }
                        else if(codeA2.getOpenStatus() == 1)
                        {
                            //关门
                            if(mession.getEndOpenDoorTime() == null)
                            {

                            }
                            else
                            {
                                //关门
                                mession.setEndCloseDoorTime(codeA2.getCreatedTime());
                                eo = 1;
                                messionDao.save(mession);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                mession.setYiChang(2);
            }
            messionDao.save(mession);
        }
    }
}
